#include <string.h>
#include <stdbool.h>
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "queue.h"
#include "delay.h"
#include "bsp_usart.h"
#include "bsp_gpio.h"
#include "bsp_can2.h"
#include "switch.h"
#include "soc.h"
#include "RS7_ABSencoder.h"
#include "K100_CD.h"
#include "crc8.h"
#include "servercomm.h"
#include "communication.h"
#include "usarttransmitter.h"
#include "MasterAppReset.h"
#include "myTask.h"
#include "armmotor.h"
#include "manage.h"
#include "armaction.h"
#include "oled_ctrl.h"
#include "oled.h"
#include "ThreadPool.h"

/***************************************************
描述：光电检测任务，用于电机光电限位
pvParameters：未调用
****************************************************/
void LightLimit_task(void *pvParameters)
{
	while(1)
	{
		ArmMotorLimitCtrl();     //光电检测
		hard_watchdog_feed();    //硬件喂狗		
        vTaskDelay(10);
	}
}

/***************************************************
描述：电机控制任务，控制电机运动
pvParameters：未调用
****************************************************/

void step_motor_task(void *pvParameters)
{
	while(1)
	{
	    ArmMotorCtrl();
		green_led_blink();
        vTaskDelay(10);
	}
}

/***************************************************
描述：通讯任务，读取队列中的数据进行解码，打包通用数
      据到队列。
pvParameters：未调用
****************************************************/
void comunication_task(void *pvParameters)
{
	extern QueueHandle_t Usart1_Rx_Queue;
    extern QueueHandle_t Usart2_Rx_Queue;	//串口2接收信息队列句柄，用于中断和任务通讯
    extern QueueHandle_t Usart3_Rx_Queue;	//串口3接收信息队列句柄，用于中断和任务通讯
    extern QueueHandle_t Usart6_Rx_Queue;	//串口6信息队列句柄，用于中断和任务通讯
    extern QueueHandle_t CAN2_Rx_Queue;  	//CAN2信息队列句柄	
	uint8_t usart1queuebuf[USART1_RX_SIZE]      = {0};	
	uint8_t serverqueuebuf[USART2_MAX_RECV_LEN] = {0};
	uint8_t usart3queuebuf[USART3_MAX_RECV_LEN] = {0};
	uint8_t usart6queuebuf[USART6_MAX_RECV_LEN] = {0};
	CanRxMsg CAN2Msg;
	uint8_t server_uploda_cnt = 5;
	while(1)
	{
		if(xQueueReceive(Usart1_Rx_Queue,usart1queuebuf,10)==pdTRUE){
			#if (HANGAR_MODE == 1)
				UsartProcess(usart1queuebuf,USART1_RX_SIZE);//解析处理串口1调试数据
				IapJump2Bootloader(serverqueuebuf);
			#elif (HANGAR_MODE == 2)
				Decoding_ServerData_By_RTU(usart1queuebuf,USART1_RX_SIZE);//解析服务器数据
			#endif
		}
		
		if(xQueueReceive(Usart2_Rx_Queue,serverqueuebuf,10)==pdTRUE){
			Decoding_ServerData_By_cJson((const char*)serverqueuebuf);
			Decoding_ServerData_By_RTU(serverqueuebuf,USART2_MAX_RECV_LEN);         //解析服务器数据
			IapJump2Bootloader(serverqueuebuf);
		}
		
		if(xQueueReceive(Usart3_Rx_Queue,usart3queuebuf,10)==pdTRUE){
			ModbusSensor_Fun.ModbusSensor_Prase(usart3queuebuf,USART3_MAX_RECV_LEN);//解析Modbus数据
		}
		
		if(xQueueReceive(Usart6_Rx_Queue,usart6queuebuf,10)==pdTRUE){
			Rs7abs_fun.RS7ABS_Prase(usart6queuebuf,USART6_MAX_RECV_LEN);            //解析绝对式编码器数据
//			Rs7abs_fun.RS7ABS_Get_Abs();   //？？
		}
		if(xQueueReceive(CAN2_Rx_Queue,&CAN2Msg,10)==pdTRUE)
		{
			K100CD_Data_Parse(CAN2Msg.StdId,CAN2Msg.Data);
		}
		if(Wait_Singal(&server_uploda_cnt,10))                                        //500ms打包一次数据
		{
			Sensor_Data_Packed_Upload_cJson();
		}
		vTaskDelay(100);
	}
}

/***************************************************
描述：传感器任务，发送询问码到各个传感器
pvParameters：未调用
****************************************************/
void sensor_task(void *pvParameters)
{
	uint8_t rs_flag = 1;
	uint8_t cnt = 0;
	const uint8_t SENSOR_TASK_TIME = 100;//时基为100ms
	while(1)
	{	
        ModbusSensorDataAsk(SENSOR_TASK_TIME);

		rs_flag ? (Rs7abs_fun.RS7ABS_Data_Ask(1),rs_flag = 0):
		          (Rs7abs_fun.RS7ABS_Data_Ask(2),rs_flag = 1);

		if(Wait_Singal(&cnt,(500/SENSOR_TASK_TIME))){
			SwitchTest();
		}
		red_led_blink();
		vTaskDelay(SENSOR_TASK_TIME);
	}
}

/***************************************************
描述：管理任务
pvParameters：未调用
****************************************************/
void manage_task(void *pvParameters)
{
	uint8_t offline_time = 0;
	Soc_fun.Soc_Init();
	extern PoolHandle_t  Hangar_Task_Pool;
	AddThreadPoolTask(Hangar_Task_Pool,OLED_HangarStart_Task,NULL);
	while(1)
	{	
		RGB_Manage();	
		if(Wait_Singal(&offline_time,40)) OffLine_Manage();   //200ms进行一次离线管理任务
		if(Door_Move_Flag()) Hangar_Door_State(); 
		Button_Control_Door();
		Button_Control_Clip();
		Button_Control_Charge();
//		Camera_Manage();
		Flash_Manage();
		Charge_SOC_Manager();
		OnlineCheck_220V();//市电掉电检测和空调控制
		Task_TimeOut_Manager();
		vTaskDelay(50);	
	}
}


//uint32_t Debug_size = 0x00;
void send_data_task(void *pvParameters)
{
	extern QueueHandle_t Usart1_Tx_Queue;  
	extern QueueHandle_t Usart2_Tx_Queue;
	extern QueueHandle_t CAN2_Tx_Queue;
	
	char usart1_tx[USART1_TX_SIZE] = "0";
	char usart2_tx[USART2_MAX_SEND_LEN] = "0";
	uint8_t can2_tx[8] = {0x00};
	while(1)
	{
		if(xQueueReceive(Usart1_Tx_Queue,usart1_tx,10) == pdPASS)
	    {
    		send_data_dma_u1((uint8_t*)usart1_tx,strlen(usart1_tx));
      	}
	    if(xQueueReceive(Usart2_Tx_Queue,usart2_tx,10)==pdTRUE)
	    {
	    	u2_printf("%s\r\n",usart2_tx);
     	}
		if(xQueueReceive(CAN2_Tx_Queue,can2_tx,10) == pdPASS)
	    {
    		CAN2_TX_PACKET(K100TX_CANID,can2_tx,8);
      	}
		
//		Debug_size = xPortGetFreeHeapSize();
//		Debug_size = uxTaskGetStackHighWaterMark(NULL);
		vTaskDelay(40);
	}
}


